package com.example.contentclient.Util;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.contentclient.PermissionLazyActvity;

public class PermissionUtil {
    //检查多个权限，如果返回true表示已完全启用权限，返回false表示未完全启用权限
    public static boolean checkPermission(Activity activity, String[] permissions, int requestCode) {
        //判断安卓版本是否在6.0以上，否则不需要动态请求权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int check = PackageManager.PERMISSION_GRANTED;
            for (String permission : permissions) {
                check = ContextCompat.checkSelfPermission(activity, permission);
                if (check != PackageManager.PERMISSION_GRANTED)
                    break;
            }
            //未开启该权限，则请求系统弹窗，好让用户选择是否立即开启权限
            if (check != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity, permissions, requestCode);
                return false;
            }
        }
        return true;

    }
    public static boolean checkGrant(int[] grantResults)
    {
        if(grantResults!=null)
        {
            //遍历权限结果数组中的每条选择结果
            for (int grantResult : grantResults) {
                if(grantResult!=PackageManager.PERMISSION_GRANTED)
                {
                    return  false;
                }
            }
            return true;
        }
        return false;
    }
}
